package c15;

import java.util.ArrayList;
import java.util.List;

public class SyncCounterThread extends Thread {
    Counter counter;

    public SyncCounterThread(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            counter.incr();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        List<Thread> list = new ArrayList<>();
        Counter counter = new Counter();
        for (int i = 0; i < 1000; i++) {
            SyncCounterThread thread = new SyncCounterThread(counter);
            thread.start();
            list.add(thread);
        }

        for (Thread t : list) {
            t.join();
        }

        System.out.println(counter.getCount());
    }
}
